package buildinjava.ant;

import buildinjava.BuildException;
import buildinjava.Project;
import org.apache.tools.ant.Task;

/**
 * Base class that all Ant integration tasks should extend.
 *
 * @author Gili Tzabari
 */
public abstract class AbstractAntTask extends Task
{
	private String projectName;

	/**
	 * Sets the project name.
	 *
	 * @param project the project name
	 * @throws IllegalArgumentException if project is null
	 */
	public void setProject(String project) throws IllegalArgumentException
	{
		if (project == null)
			throw new IllegalArgumentException("project my not be null");
		this.projectName = project;
	}

	@Override
	public final void execute() throws BuildException
	{
		if (projectName == null)
			throw new BuildException("project must be set: " + getClass().getName());
		onExecute(InjectorProvider.getInjector().getInstance(ProjectProvider.class).getProject(projectName));
	}

	/**
	 * Invokes the task.
	 *
	 * @param project the project
	 * @throws BuildException if an expected build error occurs
	 */
	protected abstract void onExecute(Project project) throws BuildException;
}
